package com.hexiang.rocketmq.mq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class TagFilterProducer {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    public void sendMsgByTag() {
        try {
            String[] tags = new String[]{"TagA", "TagB", "TagC"};

            for (int i = 0; i < 3; i++) {
                Message message = MessageBuilder.withPayload("Hello world").build();
                SendResult sendResult = rocketMQTemplate.syncSend("test_tag_filter_spring_topic" + ":" + tags[i % tags.length], message);
                log.warn("sendResult={} ", sendResult);
            }
        } catch (Exception e) {
            log.error("sendTagMsgErr", e);
        }
    }

}